/**
 * @desc 将xml数据转换成对象形式
 * @param ctx 请求上下文
 */
const getRawBody = require('raw-body')
const xml2js = require('xml2js')

function parseXML(xml) {
  return new Promise((resolve, reject) => {
    xml2js.parseString(
      xml,
      { trim: true, explicitArray: false, ignoreAttrs: true },
      function (err, result) {
        if (err) {
          return reject(err)
        }
        resolve(result.xml)
      }
    )
  })
}

function getFormatXml(ctx) {
    return new Promise(async (resolve, reject) => {
        const xml = await getRawBody(ctx.req, {
            length: ctx.request.length,
            limit: '1mb',
            encoding: ctx.charset,
        })
        console.log('xml信息',xml.toString())
        let obj = await parseXML(xml)
        resolve(obj)
    })
}

module.exports = getFormatXml
